//
//  Status.swift
//  SinaWeiBo
//
//  Created by tuyue on 16/3/13.
//  Copyright © 2016年 xueyang.co.ltd. All rights reserved.
//

import UIKit
import AFNetworking

class Status: NSObject {
/**属性*/
    /**创建时间*/
    var created_at: String?
    /**微博id*/
    var id:Int64 = 0
    /**微博正文*/
    var text: String?
    /**微博来源*/
    var source: String?
    /**创建属性user*/
    var user:User?
    /**转发微博*/
    var retweeted_status: Status? 
    /**图片来源网址*/
    var pic_urls: [[String : String]]? {
        didSet{
            
            guard let urls = pic_urls else {
            return
            }
            
        imageURLs = [NSURL]()
            for item in urls {
                let urlString = item["thumbnail_pic"]
                let url = NSURL(string: urlString!)!
                imageURLs?.append(url)
            }
            
        }
    }
    
    /**图片网址*/
    var imageURLs : [NSURL]?
    /**对user进行处理,重写方法 对字典转对象的特殊处理*/
    override func setValue(value: AnyObject?, forKey key: String) {
//        print(value,"-----")
        if key  == "user" {
            guard let dict = value as? [String : AnyObject] else {//不能转换为模型
                 return
            }
            
            user = User(dict: dict)
            return
        }
        
        if key == "retweeted_status" {
            guard let dict = value as? [String : AnyObject] else {
            return
            }
            
            retweeted_status = Status(dict: dict )
            
            return
        }
        
          super.setValue(value, forKey: key)
    }
    
    
    
    /**重写init方法，将字典转模型的KVC写入，作为初始化的一部分*/
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    /**避免Value为空导致崩溃，写此方法*/
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    /**重写打印方法，保证其打印出来的不是地址，而是我们设置的内容*/
    override var description: String{
        let keys = ["created_at","id","text","source"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
    
}

/**网络访问获取数据*/
extension Status{

    class func loadHomeData(finished: (array : [Status]?) -> ()){
        /**判断token是否过期*/
        guard let token = UserAccount.sharedAccount.access_token else {
         print("用户未登录或登陆过期")
            return
        }
        
        /**实例化AFHTTPSessionManager对象*/
        let AFN = AFHTTPSessionManager()
        
        let urlStirng = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = ["access_token":token]
        
        /**发起get请求*/
        AFN.GET(urlStirng, parameters: parameters, success: { ( _ , result ) -> Void in
            /**判断，转换为字典类型。如果是字典类型就必须，不是就return*/
            guard let dict = result as? [String : AnyObject] else {
                print("数据结构错误")
                finished(array: nil)
            return
            }
            
            /**判断，转换为数组类型。如果是数组类型就必须，不是就return*/
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                print("数据结构错误")
                finished(array: nil)
                return
            }
            
            /**创建临时数组用于接收遍历的数据*/
            var tempArray = [Status]()
            
            /**数组的遍历*/
            for item in array {
//                print(item)
              /**字典转模型*/
                let s = Status(dict: item)
//                print(s)
                /**添加到数组*/

                tempArray.append(s)
            }
            
            finished(array: tempArray)
            /**刷新页面*/

            
            }) { (_, error) -> Void in
                finished(array: nil)
                print(error)
        }
        
        
    }


}